package net.kosev.battleships.adapters;

import net.kosev.battleships.R;
import net.kosev.battleships.Utils;
import net.kosev.battleships.model.Board;
import net.kosev.battleships.model.Field;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * Grid adapter for player's board grid
 * 
 * @author Nikola
 *
 */
public class PlayerBoardAdapter extends BoardAdapter {

	/**
	 * Constructor
	 * 
	 * @param context	Activity context
	 * @param fields	Fields array
	 */
	public PlayerBoardAdapter(Context context, int[][] fields) {
		super(context, fields);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            int imageSize = Math.round(Utils.conv(mContext, 10));
            imageView.setLayoutParams(new GridView.LayoutParams(imageSize, imageSize));
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }

        switch (mFields[position / Board.SIZE][position % Board.SIZE]) {
        case Field.WATER:
        	imageView.setImageResource(R.drawable.transparent);
        	break;
        case Field.HIT:
        	imageView.setImageResource(R.drawable.crash_mini);
        	break;
        case Field.MISS:
        	imageView.setImageResource(R.drawable.miss_mini);
        	break;
        case Field.SHIP:
        	imageView.setImageResource(R.drawable.ship_mini);
        	break;
        }
        
        return imageView;
	}

}
